package com.fancy.framework
{
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.utils.Dictionary;

	public class BundleContext
	{
		private var _mainSp:Sprite;
		
		public function get loaderInfo():LoaderInfo
		{
			if(_mainSp) { return _mainSp.loaderInfo; }
			return null;
		}
		
		public function get stage():Stage
		{
			if(_mainSp) { return _mainSp.stage; }
			return null;
		}
		
		public function BundleContext(mainSp:Sprite)
		{
			_mainSp = mainSp;
			preInjectCfg();
		}
		
		//------------------------------------------------------------------------
		//-------------------------------获取参数-----------------------------------
		
		/** 程序内部版本标记 */
		public static const CFG_INSIDE_VERSION:String = "iversion";
		/** 程序外部版本标记 */
		public static const CFG_OUTSIDE_VERSION:String = "oversion";
		/** 目标IP */
		public static const CFG_DEST_IP:String = "destIP";
		/** 跨域socket连接端口 */
		public static const CFG_DOMAIN_PORT:String = "domainPort";
		/** 主连接socket连接端口 */
		public static const CFG_CONN_PORT:String = "connPort";
		
		private var _cfgDic:Dictionary = new Dictionary();
		
		/** 获取配置参数
		 * 优先从html中的flashvar参数中获取
		 * 如果获取不到，则返回_dic中的对应参数  */
		public function getConfig(key:String):Object
		{
			if(loaderInfo.parameters[key]) { return loaderInfo.parameters[key]; }
			return _cfgDic[key];
		}
		
		/** 预写入配置参数 */
		private function preInjectCfg():void
		{
			_cfgDic[CFG_INSIDE_VERSION] = "Trial_1_1";
		}
		
		/** 主动写入配置参数 */
		public function injectCfg(key:String, val:Object):void
		{
			_cfgDic[key] = val;
		}
		
	}
}